package com.quan.system.web.controller;

import com.quan.commons.core.MyBaseController;
import com.quan.commons.tools.mail.MailTools;
import com.quan.commons.tools.mail.vo.MailVo;
import com.quan.commons.vo.R;
import com.quan.system.commons.vo.SysSchedulerVo;
import com.quan.system.service.SysSchedulerService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;


/**
 * 系统工具
 */
@Slf4j
@RestController
@RequestMapping("/system/tools")
public class SysToolsController extends MyBaseController {

    @Autowired
    private MailTools mailTools;

    @Autowired
    private SysSchedulerService schedulerService;


    /**
     * 发送邮件
     *
     * @param vo
     * @return
     */
    @PostMapping("sendMail")
    public R sendMail(@Valid @RequestBody MailVo vo) {
        this.mailTools.send(vo);

        return R.ok();
    }


    /**
     * 定时任务
     * @return
     */
    @PostMapping("settingScheduler")
    public R settingScheduler() {
        Map<String, Object> jobDataMap = new HashMap<String, Object>();
        jobDataMap.put("k1", "v1");
        jobDataMap.put("k2", "v2");


        SysSchedulerVo vo = new SysSchedulerVo();
        vo.setJobName("测试定时任务");
        vo.setJobDesc("任务作业描述");
        vo.setJobDataMap(jobDataMap);
        vo.setJobParams("{'k1': 'v1', 'k2': 'v2'}");
        vo.setJobCron("0/5 * * * * ? *");
        vo.setJobClassPath("com.quan.system.scheduler.TestSchedulerJob");

        try {
            this.schedulerService.startJob(vo);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

        return R.ok();
    }

}
